package com.example.teacherservice.repository;


import com.example.teacherservice.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {

    @Query("SELECT s FROM Student s WHERE s.stuName LIKE %:keyword% OR CAST(s.userId AS string) LIKE %:keyword%")
    List<Student> searchStudents(@Param("keyword") String keyword);

    // 新增方法：根据userId查询学生信息
    Optional<Student> findByUserId(Long userId);

    // 新增方法：根据userId查询学生信息并包含用户信息
    @Query("SELECT s FROM Student s JOIN FETCH s.user u WHERE s.userId = :userId")
    Optional<Student> findWithUserByUserId(@Param("userId") Long userId);

    // 根据ID查询学生
    Optional<Student> findById(Long id);
}